<?php
namespace app\api\controller\v1;

use app\api\service\TokenService;
use app\api\validate\IDPostiveInt;
use app\api\model\Strategy as StrategyModel;
use app\api\model\Advert as AdvertModel;
use app\api\model\StrategyType as StrategyTypeModel;
use app\api\model\Banner as BannerModel;
use app\lib\exception\BaseException;
use app\api\validate\StrategyValidate;
use app\api\validate\StrategyTypeValidate;
use app\api\validate\TypeValidate;
use think\Validate;
use think\Db;

//新闻类
class Strategy extends Base
{
    //获取所有文章猎豹
    public function get_strategy_list($page=1, $size = 10,$key='')
    {
        $get = input('get.');
        if(!isset($get['type']) || empty($get['type']))
            $get['type'] = '1';
        if(!isset($get['page']) || empty($get['page']))
            $get['page'] = '1';
        if (isset($get['keywords']) && !empty($get['keywords']))
            $key = trim($get['keywords']);
        $where['is_hidden'] = '0';
        $where['type'] = $get['type'];
        $list =  ArticleModel::getarticleByPage($page,$size,$key,$where);
        if ($list->isEmpty())
        {
            $data = [
                'current_page' => 1,
                'data' => [],
                'total' => $list->total(),
            ];
        }else
            $data = $list->toArray();
        return json($data,201);

    }

    //获取某文章详情
    public function get_strategy_info($id)
    {
        (new IDPostiveInt)->goCheck();
        return StrategyModel::with(['category_content'])->where(['id'=>$id])->find();
    }

    //添加文章
    public function add()
    {
        $validate = new StrategyValidate();
        $validate->goCheck();
        $post = input('post.');//过滤非验证器中的字段
        $post['author'] = TokenService::getAdminName();
        $res = StrategyModel::CreateOne($post);
        if(!$res){
            throw new BaseException(['msg'=>'添加失败']);
        }
        return 1;
    }

    //删除文章
    public function del($id)
    {
        (new IDPostiveInt)->goCheck();
        Db::startTrans();
        try {
            StrategyModel::destroy($id);
            StrategyTypeModel::where('strategy_id','=',$id)->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();// 回滚事务
            throw new BaseException(['msg' => '删除失败']);
        }
        return 1;

    }

    //添加文章二级类型
    public function add_type()
    {
        $validate = new StrategyTypeValidate();
        $validate->goCheck();
        $post = $validate->getDataByRule(input('post.'));//过滤非验证器中的字段
        $res = StrategyTypeModel::create($post);
        if(!$res){
            throw new BaseException(['msg'=>'添加失败']);
        }
        return 1;
    }

    //获取二级分类
    public function get_strategy_type()
    {
        $validate = new TypeValidate();
        $validate->goCheck();
        $get = input('get.');//过滤非验证器中的字段
        return StrategyTypeModel::getListAll($get);
    }

    //删除二级分类
    public function del_strategy_type($id)
    {
        (new IDPostiveInt)->goCheck();
        $res = StrategyTypeModel::destroy($id);
        if(!$res){
            throw new BaseException(['msg'=>'删除失败']);
        }else{
            return 1;
        }
    }

    //小程序攻略详情
    public function strategy_detail($type){
        $res['strategy'] = StrategyModel::with(['category_content'])->where(['type'=>$type])->find();
        $res['advert'] = AdvertModel::where(['type'=>$type])->find();
        return json($res);
    }
}
